/**
 * Copyright (c) 2011-2012, James Zhan 詹波 (jfinal@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jfinal.core;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Convert String to other type object.
 */
final class TypeConverter {
	
	private static final int timeStampLen = "2011-01-18 16:18:18".length();
	private static final String timeStampPattern = "yyyy-MM-dd HH:mm:ss";
	private static final String datePattern = "yyyy-MM-dd";
	
	public static final Object convert(Class<?> clazz, String s) throws ParseException {
		if (clazz == String.class) {
			return ("".equals(s) ? null : s);
		}
		s = s.trim();
		if ("".equals(s)) {
			return null;
		}

		Object result = null;
		if (clazz == Integer.class || clazz == int.class) {
			result = Integer.parseInt(s);
		}
		else if (clazz == Long.class || clazz == long.class) {
			result = Long.parseLong(s);
		}
		else if (clazz == java.util.Date.class) {
        	if (s.length() >= timeStampLen) {
        		result = new SimpleDateFormat(timeStampPattern).parse(s);
        	}
			else {
				result = new SimpleDateFormat(datePattern).parse(s);
			}
        }
        else if (clazz == java.sql.Date.class) {
        	if (s.length() >= timeStampLen) {
        		result = new java.sql.Date(new SimpleDateFormat(timeStampPattern).parse(s).getTime());
        	}
        	else {
        		result = new java.sql.Date(new SimpleDateFormat(datePattern).parse(s).getTime());
        	}
        }
        else if (clazz == java.sql.Time.class) {
        	result = java.sql.Time.valueOf(s);
		}
        else if (clazz == java.sql.Timestamp.class) {
        	result = java.sql.Timestamp.valueOf(s);
		}
        else if (clazz == Double.class) {
        	result = Double.parseDouble(s);
		}
        else if (clazz == Float.class) {
        	result = Float.parseFloat(s);
		}
        else if (clazz == Boolean.class) {
        	result = Boolean.parseBoolean(s);
		}
        else if (clazz == java.math.BigDecimal.class) {
        	result = new java.math.BigDecimal(s);
		}
        else if (clazz == byte[].class) {
			result = s.getBytes();
		}
		else {
			if (Config.getConstants().getDevMode())
				throw new RuntimeException("Please add code in " + TypeConverter.class  + ". The type can't be converted: " + clazz.getName());
			else
				throw new RuntimeException(clazz.getName() + " can not be converted, please use other type of attributes in your model!");
        }
		
		return result;
	}
}

